﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Profile;
using System.Security.Principal;

namespace BaoJian
{
    public class Global : System.Web.HttpApplication
    {
        private WebProfile Profile
        {
            get { return new WebProfile(Context.Profile); }
        }

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpCookie authCookie;
            FormsAuthenticationTicket authTicket;
            string[] roles;
            FormsIdentity identity;
            GenericPrincipal principal;

            authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];


            if (authCookie != null)
            {
                authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                if (!authTicket.Expired)
                {
                    if (authTicket.UserData != String.Empty)
                    {
                        roles = authTicket.UserData.Trim().Split('|'); ;
                    }
                    else
                    {
                        roles = new string[1];
                    }

                    identity = new FormsIdentity(authTicket);
                    principal = new GenericPrincipal(identity, roles);
                    Context.User = principal;
                }
            }        
        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
      
   
        public void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)
        {
            WebProfile webProfile = Profile.GetProfile(e.AnonymousID);

            if (webProfile.CustomerSelect == null)
                return;
            if (Profile.CustomerSelect == "")
            {
                Profile.CustomerSelect = webProfile.CustomerSelect;
                webProfile.CustomerSelect = "";               
            }

        }
    }
}